<?php

!defined('IN') && exit('Access denied');

if (AJAX) {
    if ($_POST['opt'] == 'getaddress') {
        $address = $_ENV['db']->fetchAll("SELECT * FROM region WHERE `parent_id` = '".(int)$_POST['object_id']."'");
        echo json_encode($address);
    } else {
        $data = getRequests(array('realname','address','zip','hregion','tel','mobile'));

        if (!$data['realname']) {
            outputJson(0,'请填写姓名');
        }

        if (strlen($data['hregion'].$data['address']) > 255) {
            outputJson(0,'地址过长');
        }

        if (!preg_match('/^\d+$/',$data['tel']) or strlen($data['tel']) > 11) {
            outputJson(0,'座机联系方式填写应为7或11位纯数字');
        }

        if (!preg_match('/^\d+$/',$data['mobile']) or strlen($data['mobile']) > 11) {
            outputJson(0,'座机联系方式填写应为7或11位纯数字');
        }

        if (!preg_match('/^\d+$/',$data['zip']) or strlen($data['zip']) > 6) {
            outputJson(0,'邮编应为6位纯数字');
        }

        $data['address'] = $data['hregion'].$data['address'];
        unset($data['hregion']);

        $sql = "UPDATE customer_leader ".buildSql($data)." WHERE customer_id = '{$_G['uid']}'";
        $_ENV['db']->runSql($sql);
        outputJson(1,'操作成功');
    }
    exit;
} else {
    // 获取用户的基本信息
    $user = $_ENV['db']->fetch("SELECT * FROM customer_leader WHERE `customer_id` = '{$_G['uid']}'");
    $template->user = $user;
    // 加载selector
    $template->addScript('cselector');
    // 获取地区信息
    $address = $_ENV['db']->fetchAll("SELECT * FROM region WHERE `parent_id` = '0'");
    $template->address = $address;
}